package com.wzy.leecode;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/10/31/9:53
 * @Description: 寻找两个正序数组的中位数
 */
public class FindZhongWeiShu {

    public static void main(String[] args) {
        int[] arr1 = {1, 2};
        int[] arr2 = {3, 4};
        double medianSortedArrays = findMedianSortedArrays(arr1, arr2);
        System.out.println(medianSortedArrays);
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length = nums1.length;
        int length1 = nums2.length;
        int[] num = new int[length + length1];
        int i = 0, j = 0, k = 0;
        while (i < length && j < length1) {
            if (nums1[i] < nums2[j]) {
                num[k++] = nums1[i++];
            } else {
                num[k++] = nums2[j++];
            }
        }
        while (i < length) {
            num[k++] = nums1[i++];
        }
        while (j < length1) {
            num[k++] = nums2[j++];
        }
        System.out.println(Arrays.toString(num));
        int len = num.length;
        if (len % 2 == 0) {
            return (double) (num[len / 2] + num[len / 2 - 1]) / 2;
        }
        return num[len / 2];
    }
}
